package com.dong.controller;

import com.dong.entity.Account;
import com.dong.entity.Orders;
import com.dong.service.AccountService;
import com.dong.service.IAccountService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("account")
public class AccountController {
    @Autowired
    private IAccountService accountService;

    private Map<String,Object> map=new HashMap<>();
    /**
    * @Description: 
    * @Param: [page, size]
    * @return: org.springframework.web.servlet.ModelAndView
    * @Author: lld
    * @Date: 2020/7/19
    **/
    @RequestMapping("list")
    public ModelAndView list(@RequestParam int page, @RequestParam int size){
        List<Account> accounts = accountService.findAll(page, size);
        PageInfo pageInfo = new PageInfo(accounts);
        ModelAndView mv=new ModelAndView();
        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("list");
        return mv;
    }
    @RequestMapping("save")
    public String list(Account account){
        accountService.saveAccount(account);
//        response.sendRedirect(request.getContextPath()+"/account/list");
        return "redirect:list?page=1&size=5";
    }
    @RequestMapping("/checkName")
    @ResponseBody
    public Map<String,Object> checkName(@RequestParam("name") String name){
        System.out.println(name);
        System.out.println(accountService.checkName(name));
       if( accountService.checkName(name)!=null){
            map.put("userExist", true);
           map.put("msg", "该用户名已经存在! 请修改后重试!");
           return map;
       }else{
           map.put("userExist", false);
           map.put("msg", "用户名可用");
       }
           return map;

    }
    @RequestMapping("delete")
    public void delete(int id, HttpServletRequest request, HttpServletResponse response) throws IOException {
        accountService.delete(id);
        response.sendRedirect(request.getContextPath()+"/account/list?page=1&size=5");
        return ;
    }
    @RequestMapping("findById")
    public String findById(int id,Model model){
        Account account1 = accountService.findById(id);
        model.addAttribute("account1", account1);
        return "update";
    }
    @RequestMapping("update")
    public void update( Account account,HttpServletRequest request, HttpServletResponse response) throws IOException {
        accountService.update( account);
        response.sendRedirect(request.getContextPath()+"/account/list?page=1&size=5");
        return ;
    }
    @RequestMapping(value = "findByName",method = RequestMethod.POST)
    public ModelAndView findByName(@RequestParam String name){
        //与list共用accounts，不行
        List<Account> accounts2 = accountService.findByName(name);
        ModelAndView mv = new ModelAndView();
        mv.addObject("accounts2",accounts2);
        mv.setViewName("listByName");
        return mv;
    }
    //g根据aid查询订单
    @RequestMapping("findOrdersByaid")
    public ModelAndView findOrdersByaid(int id){
        Account ordersByaid = accountService.findAllByaid(id);
        ModelAndView mv = new ModelAndView();
        mv.addObject("ordersByaid",ordersByaid);
        System.out.println(ordersByaid);
        System.out.println(ordersByaid.getOrders());
        mv.setViewName("listOrdersByaid");
        return mv;
    }
    //g根据aid查询卡信息
    @RequestMapping("findCardsByaid")
    public ModelAndView findCardsByaid(int id){
        Account cards = accountService.findAllByaid(id);
        ModelAndView mv = new ModelAndView();
        mv.addObject("cards",cards);
        System.out.println(cards);
        System.out.println(cards.getCardsList());
        mv.setViewName("listCardsByaid");
        return mv;
    }

}
